//
//  HeaderView.m
//  Mid_Project
//
//  Created by 王世界 on 2020/12/1.
//

#import "HeaderView.h"
#import "Masonry.h"
#import "EditViewController.h"
#import "Single.h"
#import "AFNetworking.h"

@implementation HeaderView

-(id)initWithFrame:(CGRect)frame{
    //重写initWithFrame时，不要忘了下面一句
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        _image = [[UIImageView alloc] init];
        _name = [[UILabel alloc] init];
        [self addSubview:_image];
        [self addSubview:_name];
        [_image mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).with.offset(15);
            make.left.equalTo(self.mas_left).with.offset(10);
            make.width.equalTo(self.mas_width).multipliedBy(0.2);
            make.height.equalTo(self.mas_width).multipliedBy(0.2);
        }];
        _image.layer.masksToBounds = YES;
//        _image.layer.cornerRadius = _image.layer.frame.size.width / 2;
        _image.contentMode = UIViewContentModeScaleToFill;
        _image.backgroundColor = [UIColor whiteColor];
        _image.layer.borderColor = [[UIColor blackColor] CGColor];
        _image.layer.borderWidth = 0.2f;
        
        [_name mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).with.offset(15);
            make.left.equalTo(self.image.mas_right).with.offset(10);
            make.width.equalTo(self.mas_width).multipliedBy(0.6);
            make.height.equalTo(self.mas_width).multipliedBy(0.2);
        }];
        
        self.name.numberOfLines = 2;
        
        _name.userInteractionEnabled=YES;
                UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTouchUpInside:)];
                  
                [_name addGestureRecognizer:labelTapGestureRecognizer];

    }
    return self;
}

//获取view的controller
- (UIViewController *)viewController
{
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}
//点击提交按钮
-(void) labelTouchUpInside:(UITapGestureRecognizer *)recognizer{

    EditViewController *edit = [[EditViewController alloc] init];
    edit.string = [Single sharedInstance].datalist1[0][@"Name"];
    edit.title = @"修改名称";
    edit.label = @"Name";
    
    [[self viewController].navigationController pushViewController: edit animated:YES];
    
}

@end
